package com.test.velocity;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.fqh.utils.GetPropertiesValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
* @author fqh 
* @version 创建时间：2017年12月1日 上午9:18:42
* 类说明
*/
public class GetTableStructs {
	
	private final static Logger logger=LoggerFactory.getLogger(GetTableStructs.class);
	
	public static void main(String[] args) {
			System.out.println(getTableStruct("t_sys_user").toString());

	}
	/**
	 * 
	 * @param 表名
	 * @return 返回表结构和 属性..
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	public static Map<String,String> getTableStruct(String tablename) {
		String columnName;
		String columnType;
		HashMap<String,String> map = new HashMap<>() ;
		Connection conn = null;
		try {
			String driver = GetPropertiesValue.getValue("db.properties","mysql.driver"); 
			String url = GetPropertiesValue.getValue("db.properties","mysql.url"); 
			String name= GetPropertiesValue.getValue("db.properties","mysql.uid"); 
			String password =GetPropertiesValue.getValue("db.properties","mysql.password"); 
			
			conn = getConnection(driver,url,name,password);
			DatabaseMetaData m_DBMetaData = conn.getMetaData();
		    ResultSet tableRet = m_DBMetaData.getTables(null, "%",tablename,new String[]{"TABLE"});
		    ResultSet colRet = m_DBMetaData.getColumns(null,"%", tablename,"%");
		    while(colRet.next()) {
		     columnName = colRet.getString("COLUMN_NAME");
		     columnType = colRet.getString("TYPE_NAME");
		     columnType= changeConversionType(columnType);
		     map.put(columnName, columnType); 
		    }
	    }catch (SQLException e1) {
			e1.printStackTrace();
		}
		catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {	
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			
			e1.printStackTrace();
		}
		finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return map;
		
	}
	public static String  changeConversionType(String jdbcType) {
		String javaType = new String();
		Map<String ,String > map = new HashMap<>();
		map.put("VARCHAR", "java.lang.String");
		map.put("CHAR", "java.lang.String");
		map.put("BLOB", "java.lang.byte[]");
		map.put("TEXT", "java.lang.String");
		map.put("INTEGER", "java.lang.Long");
		map.put("TINYINT", "java.lang.Integer");
		map.put("SMALLINT", "java.lang.Integer");
		map.put("MEDIUMINT", "java.lang.Integer");
		map.put("BIT", "java.lang.Boolean");
		map.put("BIGINT", "java.math.BigInteger");
		map.put("FLOAT", "java.lang.Float");
		map.put("DOUBLE", "java.lang.Double");
		map.put("DECIMAL", "java.math.BigDecimal");
		map.put("ID", "java.lang.Long");
		map.put("DATE", "java.util.Date");
		map.put("INT", "int");
		map.put("TIME", "java.sql.Time");
		map.put("DATETIME", "java.sql.Timestamp");
		map.put("TIMESTAMP", "java.sql.Timestamp");
		map.put("YEAR", "java.sql.Date");

		javaType = map.get(jdbcType);			

		return javaType;
	}
	public static Connection getConnection(String driver,String url ,String name,String password) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		Class.forName(driver).newInstance();
	   try {
			Connection conn = DriverManager.getConnection(url+"?user="+name+"&password="+password);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		
	}
}
